package com.sso.login.controller;

import com.sso.login.pojo.User;
import com.sso.login.utils.LoginCache;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;


/**
 * @author Rocky
 * @create 2023-04-01 20:35
 */
@Controller
@RequestMapping("/view")
public class ViewController {
    //跳转到登录页面
    @GetMapping("/login")
    public String toLogin(@RequestParam(required = false,defaultValue = "")  String target , HttpSession session,@CookieValue(required = false,value = "TOKEN")Cookie cookie){

        if (StringUtils.isEmpty(target)) {
            target="http://www.zkjshop.com:9010";
        }
        //如果是已经登陆的用户在此访问登录系统时，就要重定向
        if (cookie!=null){
            String value = cookie.getValue();
            User user = LoginCache.loginUser.get(value);
            if (user != null){
                return "redirect:" + target;
            }
        }
        // TODO: 要做target地址是否合法的校验,暂时先不做
        //重定向地址
        session.setAttribute("target",target);
        return "login";
    }
}
